﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using Microsoft.HomeServer.Common.Client;
using Microsoft.HomeServer.Controls;
using System.Windows.Forms;

namespace RouterControl.WhsAddIn
{
    public static class MessageListBoxHelpers
    {
        public enum Severity
        {
            Error,  // default
            Info,
            Warn,
            Success,
        }

        public static MessageListBoxItem CreateItem(Severity s, string title, string text)
        {
            return CreateItem(s, title, text, null);
        }

        public static MessageListBoxItem CreateItem(Severity s, string title, string text, string linkText)
        {
            if (string.IsNullOrEmpty(title))
                throw new ArgumentException("null or empty", "title");

            if (string.IsNullOrEmpty(text))
                throw new ArgumentException("null or empty", "text");

            // continue
            var i = new MessageListBoxItem(GetColor(s))
            {
                Title = title,
                Icon = GetIcon(s),
            };

            // add description
            i.Description.Text = text;

            // add url
            if (!string.IsNullOrEmpty(linkText))
            {
                i.Description.Text += Environment.NewLine;

                var start = i.Description.Text.Length;

                i.Description.Text += linkText;

                i.Description.LinkArea = new LinkArea(start, linkText.Length);
            }

            return i;
        }

        private static Color GetColor(Severity s)
        {
            switch (s)
            {
                case Severity.Error:
                    return CommonSettings.UnHealthyColor;

                case Severity.Info:
                    return Color.Blue;

                case Severity.Success:
                    return CommonSettings.HealthyColor;

                case Severity.Warn:
                    return CommonSettings.AtRiskColor;

                default:
                    throw new Exception("unknown severity");
            }
        }

        private static Icon GetIcon(Severity s)
        {
            switch (s)
            {
                case Severity.Error:
                    return CommonImages.StatusCritical24Icon;

                case Severity.Info:
                    return Properties.Resources.icoInfo;

                case Severity.Success:
                    return CommonImages.StatusHealthy24Icon;

                case Severity.Warn:
                    return CommonImages.StatusAtRisk24Icon;

                default:
                    throw new Exception("unknown severity");
            }
        }
    }
}
